﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Models;
using DAL;
using System.Web.Script.Serialization;
using System.Data.SqlClient;

namespace 公司CRM客户关系系统.Customer
{
    /// <summary>
    /// CustomerAll 的摘要说明
    /// </summary>
    public class CustomerAll : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string action=context.Request["action"];
            if (action == "SelectCustomer")
            {
                int pageIndex = int.Parse(context.Request["pageIndex"]);
                int pageSize = int.Parse(context.Request["pageSize"]);
                //得到传过来的值
                string CusName = context.Request["CusName"];
                string CusID = context.Request["CusID"];
                //进行分页查询
                //执行查询
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("@CusName", CusName);
                pars[1] = new SqlParameter("@CusID", CusID);

                string whenStr = "CusName like '%'+@CusName+'%' and CusID like '%'+@CusID+'%'";
                PagingList<VV_Customers> list = BaseDAL.Paging<VV_Customers>(pageSize, pageIndex, whenStr, pars,false);
                //返回结果
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(new { total = list.RecordCount, data = list });
                context.Response.Write(json);
            }
            else if (action == "SelectCustomLosts")
            {
                int pageIndex = int.Parse(context.Request["pageIndex"]);
                int pageSize = int.Parse(context.Request["pageSize"]);
                //得到传过来的值
                string CusName = context.Request["CusName"];
                string CusID = context.Request["CusID"];
                //进行分页查询
                //执行查询
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("@CusName", CusName);
                pars[1] = new SqlParameter("@CusID", CusID);

                string whenStr = "CusName like '%'+@CusName+'%' and CusID like '%'+@CusID+'%'";
                PagingList<VV_CustomLosts> list = BaseDAL.Paging<VV_CustomLosts>(pageSize, pageIndex, whenStr, pars, false);
                //返回结果
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string json = jss.Serialize(new { total = list.RecordCount, data = list });
                context.Response.Write(json);
            }
            else if (action == "updateCustomer")
            {//Customers 表的修改
                int ID = int.Parse(context.Request["ID"]);//修改的ID
                int UserID = int.Parse(context.Request["UserID"]);//客户经理的ID
                Customers cus = new Customers();
                cus.ID = ID;
                cus.UserID = UserID;
                //不需要修改的
                Customers cu=BaseDAL.Find<Customers>(ID);
                cus.CusID = cu.CusID;
                cus.CusName=cu.CusName;
                cus.CusAddress=cu.CusAddress;
                cus.CusZip = cu.CusZip;
                cus.CusFax=cu.CusFax;
                cus.CusWebsite=cu.CusWebsite;
                cus.CusLicenceNo=cu.CusLicenceNo;
                cus.CusChieftain=cu.CusChieftain;
                cus.CusBankroll=cu.CusBankroll;
                cus.CusTurnover=cu.CusTurnover;
                cus.CusBank=cu.CusBank;
                cus.CusBankNo=cu.CusBankNo;
                cus.CusLocalTaxNo=cu.CusLocalTaxNo;
                cus.CusNationalTaxNo = cu.CusNationalTaxNo;
                cus.CusDate = cu.CusDate;
                cus.CusState = cu.CusState;
                //得到全部修改数据
                int r = BaseDAL.Update(cus);
                //返回结果
                context.Response.Write(r);
            }
            else if (action == "CustomerEdit")
            {//Customers 表的修改
                int ID = int.Parse(context.Request["ID"]);
                string CusName = context.Request["CusName"];
                string CusZip = context.Request["CusZip"];
                string CusAddress = context.Request["CusAddress"];
                string CusFax = context.Request["CusFax"];
                string CusWebsite = context.Request["CusWebsite"];
                string CusLicenceNo = context.Request["CusLicenceNo"];
                string CusChieftain = context.Request["CusChieftain"];
                int CusBankroll = int.Parse(context.Request["CusBankroll"]);
                int CusTurnover = int.Parse(context.Request["CusTurnover"]);
                string CusBank = context.Request["CusBank"];
                string CusBankNo = context.Request["CusBankNo"];
                string CusLocalTaxNo = context.Request["CusLocalTaxNo"];
                string CusNationalTaxNo = context.Request["CusNationalTaxNo"];

                Customers cus = new Customers();
                cus.ID = ID;//修改的ID;
                cus.CusName = CusName;
                cus.CusAddress = CusAddress;
                cus.CusZip = CusZip;
                cus.CusFax = CusFax;
                cus.CusWebsite = CusWebsite;
                cus.CusLicenceNo = CusLicenceNo;
                cus.CusChieftain = CusChieftain;
                cus.CusBankroll = CusBankroll;
                cus.CusTurnover = CusTurnover;
                cus.CusBank = CusBank;
                cus.CusBankNo = CusBankNo;
                cus.CusLocalTaxNo = CusLocalTaxNo;
                cus.CusNationalTaxNo = CusNationalTaxNo;

                //不需要修改的
                Customers cu = BaseDAL.Find<Customers>(ID);
                cus.UserID = cu.UserID;
                cus.CusID = cu.CusID;
                cus.CusDate = cu.CusDate;
                cus.CusState = cu.CusState;

                //得到全部修改数据
                int r = BaseDAL.Update(cus);
                //返回结果
                context.Response.Write(r);
            }
            else if (action == "LinkManAdd")
            {//LinkMans 表的添加
                //往客户信息添加一条数据
                LinkMans lm = new LinkMans();
                lm.LMName = context.Request["LMName"]; ;//LMName
                lm.LMSex = context.Request["LMSex"];//LMSex
                lm.LMDuty = context.Request["LMDuty"];//LMDuty
                lm.LMOfficeNo = context.Request["LMOfficeNo"];//LMOfficeNo
                lm.LMMobileNo = context.Request["LMMobileNo"];//LMMobileNo
                lm.LMMemo = context.Request["LMMemo"];//LMMemo
                lm.CusID = context.Request["CusID"];
                //得到手影响的行数
                int r = BaseDAL.Insert(lm);
                context.Response.Write(r);
            }
            else if (action == "LinkManEdit")
            {//LinkMans 表的修改
                //拿到修改的id
                int LMID = int.Parse(context.Request["LMID"]);//交往ID 
                //药修改的内容(交往记录)
                LinkMans lm = new LinkMans();
                lm.LMID = LMID;
                lm.LMName = context.Request["LMName"]; ;//LMName
                lm.LMSex = context.Request["LMSex"];//LMSex
                lm.LMDuty = context.Request["LMDuty"];//LMDuty
                lm.LMOfficeNo = context.Request["LMOfficeNo"];//LMOfficeNo
                lm.LMMobileNo = context.Request["LMMobileNo"];//LMMobileNo
                lm.LMMemo = context.Request["LMMemo"];//LMMemo
                //不需要修改的
                LinkMans lk = BaseDAL.Find<LinkMans>(LMID);
                lm.CusID = lk.CusID;//CusID

                //得到手影响的行数
                int r = BaseDAL.Update(lm);
                context.Response.Write(r);
            }
            else if (action == "ActivitysAdd")
            {//Activitys 表的添加
                Activitys at = new Activitys();
                string CusID=context.Request["CusID"];
                at.ActDate = DateTime.Parse(context.Request["ActDate"]);//ActDate
                at.ActAdd = context.Request["ActAdd"];//ActAdd
                at.ActTitle = context.Request["ActTitle"];//ActTitle
                at.ActMemo = context.Request["ActMemo"];//ActMemo
                at.ActDesc = context.Request["ActDesc"];//ActDesc
                at.CusID = CusID;
                //每一次添加新的交往记录时，都应该执行以下代码
                BaseDAL.Select<CustomLosts>("update CustomLosts set CLState=2 where CusID='" + CusID + "' and CLState=1", null);
                //得到手影响的行数
                int r = BaseDAL.Insert(at);
                context.Response.Write(r);
            }
            else if (action == "ActivitysEdit")
            {//Activitys 表的修改
                //拿到修改的id
                int ActID = int.Parse(context.Request["ActID"]);//交往ID 
                //药修改的内容(交往记录)
                Activitys at = new Activitys();
                at.ActID = ActID;
                at.ActDate = DateTime.Parse(context.Request["ActDate"]);//ActDate
                at.ActAdd = context.Request["ActAdd"];//ActAdd
                at.ActTitle = context.Request["ActTitle"];//ActTitle
                at.ActMemo = context.Request["ActMemo"];//ActMemo
                at.ActDesc = context.Request["ActDesc"];//ActDesc

                //不需要修改的
                Activitys ac = BaseDAL.Find<Activitys>(ActID);
                at.CusID = ac.CusID;//CusID

                //得到手影响的行数
                int r = BaseDAL.Update(at);
                context.Response.Write(r);
            }
            
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}